#! /bin/sh

# Provides:          isulad
# Short-Description: light-weighted container runtime daemon
# Description:       isulad is a light-weighted container runtime daemon

set -e

# /etc/init.d/isulad: start and stop the isulad daemon

DAEMON=/usr/bin/isulad
ISULAD_ENABLE=true
ISULAD_OPTS='-s test'
ISULAD_DEFAULTS_FILE=/etc/default/isulad
ISULAD_CONFIG_FILE=/etc/isulad/daemon.json
ISULAD_PID_FILE=/var/run/isulad.pid

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

if [ -s $ISULAD_DEFAULTS_FILE ]; then
    . $ISULAD_DEFAULTS_FILE
    case "x$ISULAD_ENABLE" in
	xtrue|xfalse)	;;
	xinetd)		exit 0
			;;
	*)		log_failure_msg "Value of ISULAD_ENABLE in $ISULAD_DEFAULTS_FILE must be either 'true' or 'false';"
			log_failure_msg "not starting isulad daemon."
			exit 1
			;;
    esac
fi

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin:/usr/local/bin"

isulad_start() {
    if [ ! -s "$ISULAD_CONFIG_FILE" ]; then
        log_failure_msg "missing or empty config file $ISULAD_CONFIG_FILE"
        log_end_msg 1
        exit 0
    fi
    if /usr/bin/nohup $DARMON --pidfile $ISULAD_PID_FILE $ISULAD_OPTS &
    then
        rc=0
        sleep 1
        if ! kill -0 $(cat $ISULAD_PID_FILE) >/dev/null 2>&1; then
            log_failure_msg "isulad daemon failed to start"
            rc=1
        fi
    else
        rc=1
    fi
    if [ $rc -eq 0 ]; then
        log_end_msg 0
    else
        log_end_msg 1
        rm -f $ISULAD_PID_FILE
    fi
} # isulad_start


case "$1" in
  start)
	if "$ISULAD_ENABLE"; then
	    log_daemon_msg "Starting isulad daemon" "isulad"
	    if [ -s $ISULAD_PID_FILE ] && kill -0 $(cat $ISULAD_PID_FILE) >/dev/null 2>&1; then
		log_progress_msg "apparently already running"
		log_end_msg 0
		exit 0
	    fi
            isulad_start
        else
            if [ -s "$ISULAD_CONFIG_FILE" ]; then
                [ "$VERBOSE" != no ] && log_warning_msg "isulad daemon not enabled in $ISULAD_DEFAULTS_FILE, not starting..."
            fi
	fi
	;;
  stop)
	log_daemon_msg "Stopping isulad daemon" "isulad"
	kill -INT $(cat $ISULAD_PID_FILE) 
	log_end_msg $?
	rm -f $ISULAD_PID_FILE
	;;

  reload|force-reload)
	log_warning_msg "Reloading isulad daemon: not needed, as the daemon"
	log_warning_msg "re-reads the config file whenever a client connects."
	;;

  restart)
	set +e
	if $ISULAD_ENABLE; then
	    log_daemon_msg "Restarting isulad daemon" "isulad"
	    if [ -s $ISULAD_PID_FILE ] && kill -0 $(cat $ISULAD_PID_FILE) >/dev/null 2>&1; then
		kill -INT $(cat $ISULAD_PID_FILE)
		sleep 1
	    else
		log_warning_msg "isulad daemon not running, attempting to start."
	    	rm -f $ISULAD_PID_FILE
	    fi
            isulad_start
        else
            if [ -s "$ISULAD_CONFIG_FILE" ]; then
                [ "$VERBOSE" != no ] && log_warning_msg "isulad daemon not enabled in $ISULAD_DEFAULTS_FILE, not starting..."
            fi
	fi
	;;

  status)
	status_of_proc -p $ISULAD_PID_FILE "$DAEMON" isulad
	exit $?	# notreached due to set -e
	;;
  *)
	echo "Usage: /etc/init.d/isulad {start|stop|reload|force-reload|restart|status}"
	exit 1
esac

exit 0
